#include <interface\gl.h>
#include <cstdio>

void glPrint(font* myft,const char *fmt, ...)				// Custom GL "Print" Routine
{
    if( !myft ) return;
	char		text[256];				// Holds Our String
	va_list		ap;					// Pointer To List Of Arguments
	if (fmt == NULL)					// If There's No Text
		return;						// Do Nothing
	va_start(ap, fmt);					// Parses The String For Variables
    vsprintf(text, fmt, ap);				// And Converts Symbols To Actual Numbers
	va_end(ap);						// Results Are Stored In Text
	glPushAttrib(GL_LIST_BIT);				// Pushes The Display List Bits		( NEW )
	glListBase(myft->getBase() - 32);					// Sets The Base Character to 32	( NEW )
	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text	( NEW )
	glPopAttrib();						// Pops The Display List Bits	( NEW )
}

void glCurPrint(font* myft,unsigned int cursor,int* cursorpos, const char *fmt, ...){
    if( !myft ) return;
	char		text[256];
	va_list		ap;
	if (fmt == NULL)
		return;
	va_start(ap, fmt);
	    vsprintf(text, fmt, ap);
	va_end(ap);
	unsigned int len = strlen(text);
	glPushAttrib(GL_LIST_BIT);
	glListBase(myft->getBase() - 32);
    glCallLists((len < cursor)?len:cursor, GL_UNSIGNED_BYTE, text);
    int stack[4];
    glGetIntegerv(GL_CURRENT_RASTER_POSITION,stack);
    (*cursorpos) = stack[0] - (*cursorpos);
    if( len > cursor ){
        glCallLists(len-cursor, GL_UNSIGNED_BYTE, text+cursor);
    }
	glPopAttrib();
}

void glGetCursorPrint(font* myft,int clickpos,unsigned int* cursor,int* cursorpos, const char *fmt, ...){
    if( !myft ) return;
	char		text[256];
	va_list		ap;
	if (fmt == NULL)
		return;
	va_start(ap, fmt);
	    vsprintf(text, fmt, ap);
	va_end(ap);
	int len = strlen(text);
	glPushAttrib(GL_LIST_BIT);
	glListBase(myft->getBase() - 32);
	int stack[4];
	bool done = false;
	int prev = 0;
    glGetIntegerv(GL_CURRENT_RASTER_POSITION,stack);
	for( int i = 0; i < len; i++ ){
        if( i < len) glCallLists(1,GL_UNSIGNED_BYTE,text+i);
        glGetIntegerv(GL_CURRENT_RASTER_POSITION,stack);
        if((stack[0] >= clickpos )&&!done ){
            done = true;
            (*cursor) = i;
            (*cursorpos) = prev - (*cursorpos);
        }
        prev = stack[0];
	}
	if( !done ){
        (*cursor) = len;
        (*cursorpos) = stack[0] - (*cursorpos);
	}
	glPopAttrib();
}

void glSetRenderArea(RECT rect,int offX,int offY){
    glScissor(rect.left+offX,rect.bottom+offY,rect.right-rect.left,rect.top-rect.bottom);
}

void glDrawRect(RECT rect, int offX, int offY){
    glBegin(GL_QUADS);
        glVertex2i(rect.left+offX,rect.bottom+offY);
        glVertex2i(rect.left+offX,rect.top+offY);
        glVertex2i(rect.right+offX,rect.top+offY);
        glVertex2i(rect.right+offX,rect.bottom+offY);
    glEnd();
}

float int_to_float(int input){
    return ((float)input/(float)255);
}

void glColor(color* col){
    if( !col ) return;
    glColor4fv( col->getColorVf() );
}

void glRasterPos(int x,int y){
    if( x < 0 || y < 0 ){
        glRasterPos2i(0,0);
        glBitmap(0,0,0,0,x,y,NULL);
    }else{
        glRasterPos2i(x,y);
    }
}

void glRasterOffset(int x,int y){
    glBitmap(0,0,0,0,x,y,NULL);
}

int getRasterXi(){
    int val[4];
    glGetIntegerv(GL_CURRENT_RASTER_POSITION,val);
    return val[0];
}
int getRasterYi(){
    int val[4];
    glGetIntegerv(GL_CURRENT_RASTER_POSITION,val);
    return val[1];
}
